<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in managerEmployees"
          :key="item.id"
          :value="item.id"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click.native="hsubmmit">确定</el-button>
      <el-button size="small" @click="hclose">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployees } from '@/api/employees'
import { addDepartment, updateDepartment, departmentDatil } from '@/api/departments'
export default {
  name: 'VueHrDeptDialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    //  { id, pid, name, code }
    origenList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编码校验唯一性
    const valitCode = (rule, value, callback) => {
      // console.log(rule, value)
      // console.log(this.origenList)
      // 获取到所有的code
      let aCode = this.origenList.map(item => item.code)
      // console.log(aCode)
      if (this.isEdit) {
        aCode = this.origenList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (aCode.includes(value)) {
        callback(new Error(`部门编码：${value} 已存在`))
      } else {
        callback()
      }
    }
    // 校验部门名称同级别的唯一性
    const valitName = (rule, value, callback) => {
      // 同级别的部门，就是pid相同，父级部门的id是子部门的pid
      // console.log(value)
      let jobName = this.origenList.filter(item => item.pid === this.id).map(val => val.name)
      // console.log(jobName)
      if (this.isEdit) {
        // 编辑状态，确定点击的是哪个部门
        const dept = this.origenList.filter(item => item.id === this.id)
        console.log(dept)
        // 拿到点击的pid
        const pid = dept.pid
        jobName = this.origenList.filter(item => item.pid === pid && item.id !== this.id).map(item => item.name)
        console.log(jobName)
      }
      if (jobName.includes(value)) {
        callback(new Error(`部门编码：${value} 已存在`))
      } else {
        callback()
      }
    }
    return {
      managerEmployees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 30, message: '请输入1-30个字符', trigger: 'blur' },
          { validator: valitName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { validator: valitCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    this.loadEmployees()
    this.getDatil()
  },
  methods: {
    hclose() {
      this.$emit('close')
    },
    // 点击确定
    hsubmmit() {
      // 兜底校验
      this.$refs.deptForm.validate((valit) => {
        if (!valit) return // 校验未通过，返回
        this.isEdit ? this.editDepartment() : this.doadd()
      })
    },
    // 编辑
    async editDepartment() {
      await updateDepartment(this.form)
      // console.log(res)
      // 请求成功渲染页面，父组件渲染
      this.$emit('success')
    },
    // 添加部门请求
    async doadd() {
      // 首先要获取到pid，pid就是父级部门的id
      const params = { ...this.form, pid: this.id }
      await addDepartment(params)
      // console.log(res)
      // 请求成功再父组件触发success事件，渲染页面
      this.$emit('success')
    },
    // 查询部门详情
    async getDatil() {
      // console.log(this.id)
      if (this.isEdit) {
        const res = await departmentDatil(this.id)
        // 数据回填
        this.form = res.data
        // console.log(res)
      }
    },
    // 获取部门负责人
    async loadEmployees() {
      const res = await getEmployees()
      // console.log(res)
      this.managerEmployees = res.data
      // console.log(this.managerEmployees)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
